<template>
  <div class="login_body">
    <div class="bg" />
    <div class="box">
      <div class="title">智慧园区-登录</div>
      <el-form ref="form" :model="ruleForm" :rules="rules" class="demo-ruleForm">
        <el-form-item
          label="账号"
          prop="username"
        >
          <el-input v-model="ruleForm.username" />
        </el-form-item>

        <el-form-item

          label="密码"
          prop="password"
        >
          <el-input v-model="ruleForm.password" :type="passwordType">  <template #suffix>
            <a>
              <i
                :class="['el-icon-view', passwordType === 'password' ? 'hidden' : 'visible']"
                title="显示密码"
                @click.stop="togglePasswordVisibility"
              />
            </a>
          </template></el-input>
        </el-form-item>

        <el-form-item prop="remember">
          <el-checkbox v-model="ruleForm.remember" placeholder="请输入密码"> 记住我</el-checkbox>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" class="login_btn" @click="submit">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
// import { loginAPI } from '@/api/user'
export default {
  name: 'Login',
  data() {
    return {
      passwordType: 'password',
      ruleForm: {
        username: '',
        password: '',
        remember: ''

      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]

      }
    }
  },

  mounted() {
    // 有getItem时，在输入框回显到账号密码
    const ruleFormToken = JSON.parse(localStorage.getItem('FORM_KEY'))
    if (ruleFormToken) {
      this.ruleForm = ruleFormToken
    }
  },
  methods: {
    // 封装登录,提交登录
    async submit() {
      const FORM_KEY = 'FORM_KEY'
      const { username, password } = this.ruleForm
      // 提交检验，如果检验通过，则执行登录
      this.$refs.form.validate(async valid => {
        if (valid) {
          await this.$store.dispatch('useLogin', { username, password })
          this.$router.push('/')

          this.$message.success('登录成功')

          // 记住我，保存用户名和密码回显到输入框，如果是否，则清空
          if (this.ruleForm.remember) {
            localStorage.setItem(FORM_KEY, JSON.stringify(this.ruleForm))
          } else {
            localStorage.removeItem(FORM_KEY)
            this.ruleForm = {
              username: '',
              password: '',
              remember: ''
            }
          }
        }
      })
    },
    // 点击小图标切换显示隐藏密码
    togglePasswordVisibility() {
      this.passwordType = this.passwordType === 'password' ? 'text' : 'password'
    }
  }
}

</script>

<style scoped lang="scss">
  .login_body {
    display: flex;
  }
  .bg {
    width: 60vw;
    height: 100vh;
    background: url('~@/assets/login-bg.svg') no-repeat;
    background-position: right top;
    background-size: cover;
  }
  .box {
    margin: 200px 10% 0;
    flex: 1;
    .title {
      padding-bottom: 76px;
      font-size: 26px;
      font-weight: 500;
      color: #1e2023;
    }
    ::v-deep() {
      .ant-form-item {
        display: flex;
        margin-bottom: 62px;
        flex-direction: column;
      }
      .ant-form-item-label label {
        font-size: 16px;
        color: #8b929d;
      }
      .ant-input,
      .ant-input-password {
        border-radius: 8px;
      }
    }
  }
  .login_btn{
    width: 100%;
  }
</style>
